<?php
class search_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function paging_search($keyword, $page=0, $pageSize=0, $filterType = 1, $cateId=0) {
        $db = $this->load->database('default', TRUE);
        
        $sql = "SELECT COUNT(news.news_id) c
                FROM news INNER JOIN news_cate ON news_cate.cate_id = news.cate_id ";
        $where = "WHERE news.status='1'";
        if ($keyword != "") {
            if ($filterType & 1){
                $where.=" AND (news.title LIKE CONCAT('%',?,'%') OR news.short_desc LIKE CONCAT('%',?,'%') OR news.full_desc LIKE CONCAT('%',?,'%'))";
                $arrValues = array($keyword, $keyword, $keyword);
            }
            if ($filterType & 2){
                $where.=" AND (news.title LIKE CONCAT('%',?,'%'))";
                $arrValues[] = $keyword;
            }
            if ($filterType & 4){
                $operator = " AND ";
                if ($filterType & 2) $operator = " OR ";
                $where .= $operator;
                $where .= " (news.short_desc LIKE CONCAT('%',?,'%'))";
                $arrValues[] = $keyword;
            }
            if ($filterType & 8){
                $operator = " AND ";
                if ($filterType & 2 || $filterType & 4) $operator = " OR ";
                $where .= $operator;
                $where .= " (news.full_desc LIKE CONCAT('%',?,'%') OR news.full_desc LIKE CONCAT('%',?,'%'))";            
                $arrValues[] = $keyword;
                $arrValues[] = htmlentities($keyword);
            }
        }
        
        if ($cateId >= 1 and $cateId <= 7 ){
            $where .= " and news.cate_id=?";
            $arrValues[] = $cateId;
        }
        $sql .= $where;
        $queryCount = $db->query($sql, $arrValues);  
        $aRows = $queryCount->result();
        $iRow_Count = $aRows[0]->c;
        
        $sql = "SELECT news.news_id,news.title,news.short_desc,news.img_thumb,news.img_thumb2,news.img_thumb3,news.display_date,news_cate.url as news_cate_url,news.parent_cate_url,news_cate.url as url,news_cate.cate_id 
                FROM news INNER JOIN news_cate on news_cate.cate_id = news.cate_id ";
        $sql .= $where;
        $sql .= " ORDER BY news.news_id DESC
                  LIMIT ?,?;";
        array_push($arrValues, $page, $pageSize);
        $query = $db->query($sql, $arrValues);        
        
        return array($iRow_Count, $query->result());
    }

}
?>